/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package downloadtwitterrelations;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import twitter4j.TwitterException;

/**
 *
 * @author Ranjan
 */
public class Main {

	private static BufferedReader reader;
	private static ArrayList<String> SeedUsers = new ArrayList<String>();
	/**
     * @param args the command line arguments
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    	
        //  Check to make sure only a single file argument was provided
        if (args.length > 1 || args.length < 1)
                System.out.println ("Too many/few arguments. Correct usage: tversecrawler <twitterFollowers>");

        else if (args.length > 0){
                readFile(args[0]);                
                try {
                        reader.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                fetchTweetsByUser();
        }
    }
    /**
     ** Reads the locations file
     * @param file
     */
	private static void readFile(String file){
		String input = null;
		try{
			reader = new BufferedReader (new FileReader (file));
			input = reader.readLine();
			while (input != null){
					processTwitterUsers(input);
					input = reader.readLine();				
				}
			}
			catch (Exception e){
				System.err.println ("Error reading from file " + file + ": " + e.getMessage());
			}	
	    }

    /**
     *
     * @param input
     */
	private static void processTwitterUsers(String input){
		//Add the read content to our Array List of strings
		SeedUsers.add(input);
		//System.out.format("Locations Size %s", locations.size());
	}   
	
	private static void fetchTweetsByUser() throws Exception {

	  	for(int i=0; i<SeedUsers.size();i++) {
	           try {
	          	 System.out.format("Querying %s times", SeedUsers.size());
	      	 // TODO code application logic here
	         TwitterRelations tr = new TwitterRelations();
	         tr.startDownload(SeedUsers.get(i));
	       } catch (TwitterException e) {
	           if (e.exceededRateLimitation()) {
	               System.out.println("blocked!");
	           }
	       } catch (SQLException e) {
				// TODO Auto-generated catch block
					e.printStackTrace();
				}
		 } 
	}
}
